/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Onta��n, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				AttackBOPlan
 
Function						: 				Provides a Basic Operator Plan 
												for Attack 
****************************************************************************/
package planninglanguage.plans;

import org.jdom.Element;

import planninglanguage.conditions.AndCondition;
import planninglanguage.conditions.AttackedCondition;
import planninglanguage.conditions.Condition;
import planninglanguage.conditions.UnitExistsCondition;
import util.XMLWriter;
import base.ProxyBot;
import base.WargusUnit;

public class AttackBOPlan extends BasicOperatorPlan implements Cloneable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int m_selfPlayerID;
	int m_targetID;
	WargusUnit m_originalTargetUnit = null;
	
	public AttackBOPlan(int a_unitID,int a_targetID, int a_selfPlayerID)
	{
		super(null, null, null);
		m_selfPlayerID = a_selfPlayerID;
		m_unitID=a_unitID;
		m_targetID=a_targetID;
		m_precondition = new AndCondition(new UnitExistsCondition(m_unitID), new UnitExistsCondition(m_targetID));
		m_aliveCondition = new UnitExistsCondition(m_unitID);
		m_successCondition = new AttackedCondition(m_targetID);
		m_parent = null;
	}	
	
	public AttackBOPlan(int a_unitID,int a_targetID,int a_selfPlayerID,
			Condition a_precondition,Condition a_aliveCondition,Condition a_successCondition) {
		super(null, null, null);
		m_selfPlayerID = a_selfPlayerID;
		m_unitID=a_unitID;
		m_targetID=a_targetID;
		m_precondition = a_precondition;
		m_aliveCondition = a_aliveCondition;
		m_successCondition = a_successCondition;
		m_parent = null;
	}
	
	public AttackBOPlan(int a_unitID,int a_targetID,Condition a_precondition,Condition a_aliveCondition,Condition a_successCondition, Plan a_parent)
	{
		super(a_precondition,a_aliveCondition,a_successCondition); // XXX what is the point of this if we are setting them here
		m_unitID=a_unitID;
		m_targetID=a_targetID;
		m_precondition = new AndCondition(new UnitExistsCondition(m_unitID),new UnitExistsCondition(m_targetID));
		m_aliveCondition = new UnitExistsCondition(m_unitID);
		m_successCondition = null;
		m_parent = a_parent;
	}
	
	
	public WargusUnit getOriginalTargetUnit() {
		return m_originalTargetUnit;
	}
	
	
	public void setOriginalTargetUnit(WargusUnit wu) {
		m_originalTargetUnit = wu;
	}
	
	
	public int getTargetID() {
		return m_targetID;
	}
	
	public int getSelfPlayerID() {
		return m_selfPlayerID;
	}
	
	public Object clone()
	{
		AttackBOPlan l_p = new AttackBOPlan(m_unitID,m_targetID,null,null,null, m_parent);
		l_p.setContext(m_context);
		l_p.m_originalUnit = m_originalUnit;
		l_p.m_originalTargetUnit = m_originalTargetUnit;
		return l_p;
	}
	
	
	public boolean send()
	{
		// send the attack command to Stratagus
		/*
		WargusInterface.m_action = WargusInterface.ACTION_ATTACKBUILDING;
		WargusInterface.m_unitid = m_unitID;
		WargusInterface.m_targetid = m_targetID;
		*/
		
	    ProxyBot.getProxyBot().attack(m_unitID, m_targetID);

		return true;
	}
	
	
	public String toString(int tabs) {
		String str= "";
		int i;
		
		for(i=0;i<tabs;i++) str = str + "  ";
		str =  str + "AttackBOPlan(status = " + STATUS_NAME[m_status] + " , unitID = " + m_unitID + " targetTnitID = " + m_targetID + " ) ";
		str = str + conditionsToString(tabs);
		return str;
	}	
	
	
	public boolean sameGoal(Object other)
	{
	   if (getClass() != other.getClass()) return false;
	   
	   AttackBOPlan o = (AttackBOPlan) other;
	   
	   if (m_unitID!=o.m_unitID) return false;
	   if (m_targetID!=o.m_targetID) return false;   
	   
	   return true;
	}
	
	public void writeXML(XMLWriter w) {
		w.tag(super.XMLopenTag() + " type=\"" + 
				this.getClass().getSimpleName() + "\"");		
		{
			w.tag("player-id", m_selfPlayerID);
			
			w.tag(Plan.XML_DESTINATION);			
			{
				w.tag("target-id", m_targetID);
				WargusUnit.writeXML(w, m_originalTargetUnit);
			}
			w.tag("/" + Plan.XML_DESTINATION);
			
			super.writeBOPlanAttrsXML(w);
		}
		w.tag(super.XMLcloseTag());
	}
	protected static BasicOperatorPlan loadFromXML(Element e) {
		int playerID = Integer.parseInt(e.getChildText("player-id"));
		int targetID = Integer.parseInt(
				e.getChild(Plan.XML_DESTINATION).getChildText("target-id"));
		WargusUnit target = WargusUnit.loadFromXML(
				e.getChild(Plan.XML_DESTINATION).getChild("unit"));
		
		AttackBOPlan plan = new AttackBOPlan(-1, targetID, playerID);
		plan.setOriginalTargetUnit(target);
		return plan;
	}
}
